package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataTitle extends NullField implements ITag {
	public static String DEFAULT_ID = "_Title";
		DataString title = new DataString();
		DataString author = new DataString();
		DataString version = new DataString();
		DataText notes = new DataText();
public DataTitle() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TITLE))
		{
			title = new DataString();
	  		title.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.AUTHOR))
		{
			author = new DataString();
	  		author.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.VERSION))
		{
			version = new DataString();
	 		version.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.NOTES))
		{
			notes = new DataText();
			notes.buildFromgrfxML(element);
		}
		else if( !NameHelper.isSystemName(element.getName()) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.TITLE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TITLE))
		{
	  		title.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.AUTHOR))
		{
	  		author.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.VERSION))
		{
	 		version.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.NOTES))
		{
			notes.copyFromgrfxML(element);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.TITLE,""+element.getStartLineNo()} );
		}
	}
}
public DataString getAuthor() {
	return author;
}
public DataTitle getClone() {

	DataTitle newTitle = new DataTitle();

	newTitle.author = author.getClone();
	newTitle.notes = notes.getClone();
	newTitle.title = title.getClone();
	newTitle.version = version.getClone();

	return newTitle;
}
public DataTitle getClone(String path) {

	DataTitle newTitle = new DataTitle();

	GrfxMLStoreProcessor.setCloneName( path,this,newTitle );

	newTitle.author = author.getClone(path);
	newTitle.notes = notes.getClone(path);
	newTitle.title = title.getClone(path);
	newTitle.version = version.getClone(path);

	return newTitle;
}
public DataText getNotes() {
	return notes;
}
public DataString getTitle() {
	return title;
}
public DataString getVersion() {
	return version;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(title,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(author,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(version,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(notes,this);
	}
	catch(LockException le)
	{}//Dummy catch
	notes.register(store);
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	if(title==oldObj)
		title=(DataString)newObj;

	if(author==oldObj)
		author=(DataString)newObj;

	if(version==oldObj)
		version=(DataString)newObj;

	if(notes==oldObj)
		notes=(DataText)newObj;
}
public void resetChanged() {
	super.resetChanged();
	title.resetChanged();
	author.resetChanged();
	version.resetChanged();
	notes.resetChanged();
}
protected void setAuthor(DataString newAuthor) {
	author = newAuthor;
}
protected void setNotes(DataText newNotes) {
	notes = newNotes;
}
protected void setTitle(DataString newTitle) {
	title = newTitle;
}
protected void setVersion(DataString newVersion) {
	version = newVersion;
}
public String togrfxML(String name) {
	String xml = "<" + name + ">";
	xml += title.togrfxML(Token.TITLE);
	xml += author.togrfxML(Token.AUTHOR);
	xml += version.togrfxML(Token.VERSION);
	xml += notes.togrfxML(Token.NOTES);
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
